﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class TimKiemNangCao :XtraUserControl
    {
        private int MaChucVu;
        DataTable dt;
        public TimKiemNangCao()
        {
            InitializeComponent();
            LoadDanhSachQuan();
            LoadDanhSachLoaiDaiLy();
            fillYearCombobox();
            fillMonthCombobox();
        }

        public TimKiemNangCao(int MaChucVu)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            LoadDanhSachQuan();
            LoadDanhSachLoaiDaiLy();
            fillYearCombobox();
            fillMonthCombobox();
            this.MaChucVu = MaChucVu;
            if (MaChucVu == 1)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 2)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 3)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 4)
            {

                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 5)
            {
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
        }
        private void LoadDanhSachQuan()
        {
            List<QuanDTO> list = new List<QuanDTO>();
            list = QuanDAO.LoadDanhSachQuan();
            foreach (var quan in list)
            {
                cbbQuan.Properties.Items.Add(quan.TenQuan);
            }

        }
        private void LoadDanhSachLoaiDaiLy()
        {

            List<LoaiDaiLyDTO> list = new List<LoaiDaiLyDTO>();
            list = LoaiDaiLyDAO.LoadDanhSachLoaiDaiLy();
            foreach (var loaidaily in list)
            {
                cbbLoaiDaiLy.Properties.Items.Add(loaidaily.TenLoaiDaiLy);
            }

        }
        private void fillYearCombobox()
        {
            int year = DateTime.Now.Year;
            for (int i = 2005; i <= year; i++)
            {
                cbbNamTiepNhanDen.Properties.Items.Add(i);
                cbbNamTiepNhanTu.Properties.Items.Add(i);
            }
            cbbNamTiepNhanTu.SelectedIndex = -1;
            cbbNamTiepNhanDen.SelectedIndex = -1;
         //   cbbNamTiepNhanTu.Properties.Sorted = true;
        }
        private void fillMonthCombobox()
        {
            // int year = DateTime.Now.Year;
            for (int i = 1; i <= 12; i++)
            {
                cbbThangTu.Properties.Items.Add(i);
                cbbThangDen.Properties.Items.Add(i);
            }
            cbbThangTu.SelectedIndex = -1;
            cbbThangDen.SelectedIndex = -1;
           // cbbThangTu.Properties.Sorted = true;
           
        }
        private void TimKiem()
        {
            try
            {
                int namTu, namDen, thangTu, thangDen, noTu, noDen;
                namTu = namDen = thangTu = thangDen = noTu = noDen = -500;
                string loaidaiLy="";
                string tenQuan="";
                
                    DaiLyDTO dl = new DaiLyDTO();
                    dl.TenDaiLy = textbTenDaiLy.Text;
                    dl.DiaChi = textbDiaChi.Text;
                    if (cbbQuan.SelectedIndex != -1)
                    {
                        tenQuan = cbbLoaiDaiLy.Text;
                       
                    }
                    if (cbbLoaiDaiLy.SelectedIndex != -1)
                    {
                        loaidaiLy = cbbLoaiDaiLy.Text;
                    }
                    if (cbbNamTiepNhanTu.SelectedIndex != -1)
                    {
                        namTu = cbbNamTiepNhanTu.SelectedItem.GetHashCode();
                    }
                    if (cbbNamTiepNhanDen.SelectedIndex != -1)
                    {
                        namDen = cbbNamTiepNhanDen.SelectedItem.GetHashCode();
                    }
                    if (cbbThangTu.SelectedIndex != -1)
                    {
                        thangTu = cbbThangTu.SelectedItem.GetHashCode();
                        // thangTu =int.Parse(cbbThangTu.Text.ToString());
                    }
                    if (cbbThangDen.SelectedIndex != -1)
                    {
                        thangDen = Convert.ToInt32(cbbThangDen.SelectedItem.GetHashCode());
                    }
                    if (txtNoTu.Text != "")
                    {
                        if (Util.IsNumber(txtNoTu.Text) == false)
                        {
                            MessageBox.Show("Dữ liệu nhật vào phải là số", "Thông Báo");
                            clearField();
                        }
                        noTu = int.Parse(txtNoTu.Text);
                    }
                    if (txtNoDen.Text != "")
                    {
                        if (Util.IsNumber(txtNoDen.Text) == false)
                        {
                            MessageBox.Show("Dữ liệu nhật vào phải là số", "Thông Báo");
                            clearField();
                        }
                        noDen = int.Parse(txtNoDen.Text);
                    }
                    // DataTable dt = DaiLyDAO.TimKiemNangCao(dl, namTu, namDen, thangTu, thangDen, noTu, noDen);
                    dt = DaiLyDAO.TimKiemNangCao(dl, namTu, namDen, thangTu, thangDen, noTu, noDen, loaidaiLy, tenQuan);
                    if (dt.Rows.Count <= 0)
                    {
                        lbThongBao.Text = "Không tìm thấy đại lý như yêu cầu";
                        clearField();
                    }
                    else
                    {
                        gridDaiLyControl.DataSource = dt;
                        clearField();
                        lbThongBao.Text = "";
                    }
            }
            catch (Exception)
            {
                lbmessenger.Text = "Không tìm thấy";
            }
        }
        private void clearField()
        {
            cbbLoaiDaiLy.SelectedIndex = -1;
            cbbNamTiepNhanDen.SelectedIndex = -1;
            cbbThangDen.SelectedIndex = -1;
            cbbThangTu.SelectedIndex = -1;
             txtNoTu.Text = "";
             txtNoDen.Text = "";
            cbbNamTiepNhanTu.SelectedIndex = -1;
            cbbQuan.SelectedIndex = -1;
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();

        }
        private void gridViewDanhSachDaiLy_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
             if (e.IsGetData)
            {
                e.Value = QLDaiLy.Properties.Resources.ThuTien;
            }
            if (e.Column.Caption == "Hoá Đơn")
            {
                e.Value = QLDaiLy.Properties.Resources.HoaDon;
            }
            if (e.Column.Caption == "Cập Nhật")
            {
                e.Value = QLDaiLy.Properties.Resources.CapNhatIcon;
            }
            if (e.Column.Caption == "Xoá Giả")
            {
                e.Value = QLDaiLy.Properties.Resources.deletexoagia;
            }
            if (e.Column.Caption == "Xoá Thật")
            {
                e.Value = QLDaiLy.Properties.Resources.delete;
            }
        
        }

        private void repositoryItemHoaDon_Click(object sender, EventArgs e)
        {
            PhieuXuatDTO px = new PhieuXuatDTO();
            px.MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            // px.TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuXuat frm = new PhieuXuat(px);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemThuTien_Click(object sender, EventArgs e)
        {
            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string NoCuaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NoCuaDaiLy"]).ToString();

            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuThuTien frm = new PhieuThuTien(MaDaiLy, TenDaiLy, NoCuaDaiLy);
            tb.Controls.Add(frm, 0, 1);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void repositoryItemXoa_Click(object sender, EventArgs e)
        {
             DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
             if (dr == DialogResult.OK)
             {
                 string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                 bool kq = DaiLyDAO.deleteDaiLy(MaDaiLy);
                 if (kq)
                 {
                     // MessageBox.Show("Xoá thành công");
                     TimKiem();
                 }
                 else
                 {
                     MessageBox.Show("Xoá thất bại");
                 }
             }
        }

        private void repositoryItemXoaGia_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string maDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                bool kq = DaiLyDAO.XoaGia(maDaiLy);
                if (kq == true)
                {
                    MessageBox.Show("Xoá thành công !");
                    // dataGridView.Rows.Add();
                    TimKiem();
                }
                else
                {
                    MessageBox.Show("Có lổi khi xoá đại lý");
                }
            }
        }

        private void repositoryItemCapNhat_Click(object sender, EventArgs e)
        {

            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string TenLoaiDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenLoaiDaiLy"]).ToString();
            string TenQuan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenQuan"]).ToString();
            string DiaChi = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DiaChi"]).ToString();
            string DienThoai = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DienThoai"]).ToString();
            string Email = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["Email"]).ToString();
            string NgayTiepNhan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NgayTiepNhan"]).ToString();

            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            // CapNhatDaiLy frm = new CapNhatDaiLy(_dl);
            CapNhatDaiLy frm = new CapNhatDaiLy(MaDaiLy, TenDaiLy, TenLoaiDaiLy, TenQuan, DiaChi, DienThoai, Email, NgayTiepNhan);
            tb.Controls.Add(frm, 0, 1);
        }
    }
}
